<?php
header("Content-Type:text/html;charset=utf-8");
//使用GD库实现验证码？
function yzm($length=4){
$width=$length*100;
$height=100;
$suofang=0.8;
$bej=imagecreatetruecolor($width,$height);
$bgc=imagecolorallocate($bej,255,255,255);
$color=imagecolorallocate($bej,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));

imagefill($bej,0,0,$bgc);
for($i=1;$i<=1000;$i++){
    $color=imagecolorallocate($bej,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
    imagesetpixel($bej,mt_rand(0,$width),mt_rand(0,$height),$color);
}

for($a=1;$a<=20;$a++){
$color=imagecolorallocate($bej,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imageline($bej,mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,$width),mt_rand(0,$height),$color);
}

$rand='';
for($b=1;$b<=$length;$b++){
    $strings='0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM';
    $cd=mt_rand(0,strlen($strings)-1);
    $color=imagecolorallocate($bej,mt_rand(0,125),mt_rand(0,125),mt_rand(0,125));
    $char=$strings[$cd];
    $rand .= $char;
    setcookie('yzm',$rand,time()+30,'/');
    imagettftext($bej,mt_rand(40,70),mt_rand(-15,15),70*$b,$height*$suofang,$color,'../css/Microsoft+yahei.ttf',$char); 
}
header('Content-type:image/png');
imagePng($bej);
imagedestroy($bej);
}
?>